using System;
using System.Collections.Generic;

namespace SystemScanner.ClientServices
{
	/// <summary>
	/// Allows flexible definition of identity comparisons between two instances of
	/// an arbitary type. Useful for Linq queries using Union, Distinct, etc.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class FlexibleComparer<T> : IEqualityComparer<T>
	{
		private readonly Func<T, T, bool> _comparer;

		public FlexibleComparer(Func<T, T, bool> comparer)
		{
			if (comparer == null)
				throw new ArgumentNullException("comparer");

			_comparer = comparer;
		}

		public bool Equals(T x, T y)
		{
			return _comparer(x, y);
		}

		public int GetHashCode(T obj)
		{
			return obj.ToString().ToLower().GetHashCode();
		}
	}
}
